package com.lenovo.dil.tiltbaseddemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.lenovo.dil.tiltbaseddemo.utils.PreferenceUtil;

public class SettingsActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SettingsFragment fragment = new SettingsFragment();
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.container, fragment).commit();
	}

	public static class SettingsFragment extends Fragment implements
			View.OnClickListener{

		EditText tilt_gain, move_up_init_angle, move_down_init_angle,
				over_icon_speed_ratio, select_app_time_delay,
				move_up_down_time_delay;
		Button mSave;

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_settings,
					container, false);
			tilt_gain = (EditText) rootView.findViewById(R.id.tilt_gain);
			move_up_init_angle = (EditText) rootView
					.findViewById(R.id.move_up_init_angle);
			move_down_init_angle = (EditText) rootView
					.findViewById(R.id.move_down_init_angle);
			over_icon_speed_ratio = (EditText) rootView
					.findViewById(R.id.over_icon_speed_ratio);
			select_app_time_delay = (EditText) rootView
					.findViewById(R.id.select_app_time_delay);
			move_up_down_time_delay = (EditText) rootView
					.findViewById(R.id.move_up_down_time_delay);
			mSave = (Button) rootView.findViewById(R.id.btn_save);
			init();
			return rootView;
		}

		private void init() {
			tilt_gain.setText("" + PreferenceUtil.get_tilt_gain());
			move_up_init_angle.setText(""
					+ PreferenceUtil.get_move_up_init_angle());
			move_down_init_angle.setText(""
					+ PreferenceUtil.get_move_down_init_angle());
			over_icon_speed_ratio.setText(""
					+ PreferenceUtil.get_over_icon_speed_ratio());
			select_app_time_delay.setText(""
					+ PreferenceUtil.get_select_app_time_delay());
			move_up_down_time_delay.setText(""
					+ PreferenceUtil.get_move_up_down_time_delay());
			mSave.setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			PreferenceUtil.set_tilt_gain(Integer.parseInt(tilt_gain.getText()
					.toString()));
			PreferenceUtil.set_move_up_init_angle(Integer
					.parseInt(move_up_init_angle.getText().toString()));
			PreferenceUtil.set_move_down_init_angle(Integer
					.parseInt(move_down_init_angle.getText().toString()));
			PreferenceUtil.set_over_icon_speed_ratio(Float
					.parseFloat(over_icon_speed_ratio.getText().toString()));
			PreferenceUtil.set_select_app_time_delay(Integer
					.parseInt(select_app_time_delay.getText().toString()));
			PreferenceUtil.set_move_up_down_time_delay(Integer
					.parseInt(move_up_down_time_delay.getText().toString()));
			getActivity().finish();
		}
	}
}
